﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using SerienNewsReader.Model;
using DevTyr.Mvvm.ViewModel;
using DevTyr.Mvvm.Locator;
using DevTyr.Mvvm.Messaging;
using System.Collections.Generic;

namespace SerienNewsReaderMVVM.ViewModel
{
    /// <summary>
    /// Viewmodel for the series. Allowing to filter the received series.
    /// </summary>
    [LocatorAttribute("SeriesViewModel")]
    public class SeriesViewModel : ViewModelBase
    {
        private List<RSSFeed> allSeries = default(List<RSSFeed>);

        private ObservableCollection<RSSFeed> series = default(ObservableCollection<RSSFeed>);

        /// <summary>
        /// Initializes a new instance of class <see cref="SeriesViewModel"/>
        /// </summary>
        public SeriesViewModel()
        {
            Loader.Instance.DownloadCompleted += new Loader.DownloadCompletedDelegate(OnDownloadCompleted);
            Loader.Instance.LoadSeries();
            Messenger.Instance.Register<string>(this, this.ApplyFilter);
        }

        void OnDownloadCompleted(object sender, EventArguments.DownloadFinishedEventArgs e)
        {
            Loader.Instance.DownloadCompleted -= this.OnDownloadCompleted;
            if (e.Series != null)
            {
                this.allSeries = e.Series;
                this.Series = new ObservableCollection<RSSFeed>(e.Series);
            }
        }

        /// <summary>
        /// Applyes the filter criteria on the list of series
        /// </summary>
        /// <param name="criteria">Text for the filtering</param>
        private void ApplyFilter(string filter)
        {
            if (String.IsNullOrEmpty(filter))
            {
                return;
            }
            else
            {
                if (filter.Equals("All my series"))
                {
                    this.ApplyFilterAllMySeries();
                }
                else if (filter.Equals("All series"))
                {
                    this.Series = new ObservableCollection<RSSFeed>(this.allSeries);
                }
                else
                {
                    if (filter.Contains('.'))
                    {
                        filter = filter.Replace('.', ' ');
                    }
                    filter = filter.ToLower();

                    this.Series = new ObservableCollection<RSSFeed>(
                        this.allSeries.Where(x => x.Title.ToLower().Contains(filter)).OrderBy(
                        x => x.Title));
                }
            }
        }

        /// <summary>
        /// Applies the filter for all series that are defined in the config file
        /// </summary>
        private void ApplyFilterAllMySeries()
        {
            List<string> allSeries = SerienNewsReaderMVVM.config.Default.Series.Split(';').ToList<string>();
            List<RSSFeed> allMySeries = new List<RSSFeed>();
            foreach (string entry in allSeries)
            {
                if (string.IsNullOrEmpty(entry))
                {
                    continue;
                }
                else
                {
                    allMySeries.AddRange(
                        this.allSeries.Where(x => x.Title.ToLower().Contains(entry.ToLower())).OrderBy(x => x.Title)
                        );
                }
            }
            if (allMySeries.Count > 0)
            {
                this.Series = new ObservableCollection<RSSFeed>(allMySeries);
            }
        }

        /// <summary>
        /// Gets or sets the list of series to be displayed
        /// </summary>
        public ObservableCollection<RSSFeed> Series
        {
            get
            {
                return this.series;
            }
            set
            {
                if (value != this.series)
                {
                    this.series = new ObservableCollection<RSSFeed>(value.OrderBy(x => x.Title));
                    base.RaisePropertyChanged("Series");
                }
            }
        }
    }
}
